package ui;

import java.awt.*;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

import core.BrowserLauncher;

class ImageInfo{
	public String file = null;
	public int x = 0;
	public int y = 0;
	public ImageInfo(String filename){
		file = new String(filename);
	}
}

public class ImagePanel extends JComponent {

	//private String[] m_file = null;
	private ImageInfo[] m_file = null;
	private boolean m_fullsize = false;
	private int width = 0;
	private int height = 0;
	private String url = null;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Image[] image;  //  @jve:decl-index=0:
	
	public ImagePanel(String[] file){
		super();
		initialize();
		//m_file = new String[file.length];
		m_file = new ImageInfo[file.length];
		image = new Image[file.length];
		for (int i =0; i<file.length; i++){
			m_file[i] = new ImageInfo(file[i]);
		}
	}
	
	public ImagePanel(String[] file, boolean fullsize){
		super();
		initialize();
		m_file = new ImageInfo[file.length];
		image = new Image[file.length];
		for (int i =0; i<file.length; i++){
			m_file[i] = new ImageInfo(file[i]);
		}
		m_fullsize = fullsize;
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.addMouseListener(new java.awt.event.MouseAdapter() {
        	public void mouseClicked(java.awt.event.MouseEvent e) {
        		
        		if (SwingUtilities.isRightMouseButton(e)){
	        		for (int i=0; i<m_file.length; i++){
	        			if ((m_file[i].x <= e.getX()) && (m_file[i].x + width >= e.getX()) &&
	        					((m_file[i].y <= e.getY()) && (m_file[i].y + height >= e.getY())) ){
	        				String id = m_file[i].file.substring(m_file[i].file.lastIndexOf("/") + 1,m_file[i].file.lastIndexOf("."));
	    					url = "http://www.boardgamegeek.com/game/" + id;
	        			}
	        		}
        			JPopupMenu popup = new JPopupMenu();
        			JMenuItem menuitem = new JMenuItem();
        			menuitem.setText("BGG");
        			menuitem.addActionListener(new java.awt.event.ActionListener() {
        				public void actionPerformed(java.awt.event.ActionEvent e){
        					System.out.println(url);
        					BrowserLauncher.openURL(url);
        				}
        			});
        			popup.add(menuitem);
        			popup.show(e.getComponent(), e.getX() + 10, e.getY() + 15);

        		}
        	}
        });
	}

	public void paint(Graphics g){

		int i = 0;
		int columnnum = 0;
		
		columnnum = (int)Math.ceil(Math.pow(m_file.length,0.5));
		
		for (i=0; i<m_file.length; i++){
			if (image[i] == null){
				image[i] = getToolkit().createImage(m_file[i].file);
			}
		}
		
		width = this.getWidth() / columnnum;
		height = this.getHeight() / columnnum;//( (int)Math.ceil(m_file.length / columnnum) );
		
		int x = 0;
		int y = 0;
		int column = 0;
		for (i=0; i<m_file.length; i++){
			m_file[i].x = x;
			m_file[i].y = y;
			if (m_fullsize != true){
				g.drawImage(image[i], x, y, width, height, this);
			}else{
				g.drawImage(image[i], x, y, this);
			}
			if ( column + 1 < columnnum){
				x = x + width;
				column++;
			}else{
				column = 0;
				x = 0;
				y = y + height;
			}
		}
		

	}
}
